package com.xinyue.auto.reader.process;

import java.util.List;

import org.dom4j.Element;

import com.xinyue.auto.exception.ManyManyException;
import com.xinyue.auto.reader.Process;
import com.xinyue.auto.reader.major.RelyMajor;
import com.xinyue.auto.reader.util.SetterUtil;
import com.xinyue.auto.util.StringUtil;

public class ManyProcess extends RelyMajor{
	public ManyProcess(Process read) {
		super(read);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	protected void validate() throws Exception {
		//读取many标签
		List<Element> many = xml.elements(many_);
		if(many != null && many.size() > 0) {
			for (int i = 0; i < many.size(); i++) {
				try {
					//公共属性
					commonRead(many.get(i));
					att.setType("List<" + entity + ">");
					holder.getBeanImpt().add("java.util.List");
					
					//添加属性
					atts.add(att);
					
					//预处理查询方法
					process();
					//添加查询方法
					querys.add(query);
				} catch (Exception e) {
					throw new ManyManyException(e.getMessage(), holder.getException());
				}
			}
		}
	}
	
	@Override
	protected void process() {
		query.setReturnType("List<" + entity + ">");
		query.setMethod("query" + entity + "By" + StringUtil.firstUpper(key));
		query.setParams(queryType + " " + key);
		query.setState("from " + entity + " where " + key + "=:" + key);
		
		String[] arr = new String[1];
		arr[0] = SetterUtil.setterHQL(key, queryType);
		query.setParamSetter(arr);
		
		query.setQuery(true);
		query.setWarning(true);
	}
}